Product Code Database
Example Keywords: cap -sail $80
   » » Wiki: Null Pointer
Tag Wiki 'Null Pointer'.
Tag

In , a null pointer (sometimes shortened to nullptr or null) or null reference is a value indicating that the pointer or reference does not refer to an object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to and to the Nothing value in an . Languages that protect against null pointer accesses through the type system with explicit non-null types, are called (or void safe). In some languages, it can be considered a .

A null pointer should not be confused with an uninitialized pointer: a null pointer is guaranteed to compare unequal to any pointer that points to an object. However, in general, most languages do not offer such a guarantee for uninitialized pointers. It might compare equal to other, valid pointers; or it might compare equal to null pointers. It might do both at different times, or the comparison might be undefined behavior. Also, in languages offering such support, the correct use depends on the individual experience of each developer and linter tools. Even when used properly, null pointers are semantically incomplete, since they do not offer the possibility to express the difference between "not applicable", "not currently known", and "not yet determined" values.

In systems with a tagged architecture, a possibly null pointer can be replaced with a which enforces explicit handling of the exceptional case; in fact, a possibly null pointer can be seen as a with a computed tag.

Because a null pointer does not point to a meaningful object, an attempt to access the data stored at that (invalid) memory location may cause a run-time error or immediate program crash. This is the null pointer error, or null pointer exception. It is one of the most common types of software weaknesses, and , who introduced the concept, has referred to it as a "billion dollar mistake".


C
In C, two null pointers of any type are guaranteed to compare equal.ISO/IEC 9899, clause 6.3.2.3, paragraph 4. The preprocessor macro NULL is provided, defined as an implementation-defined null pointer constant in <stdlib.h>,ISO/IEC 9899, clause 7.17, paragraph 3: NULL... which expands to an implementation-defined null pointer constant... which in C99 can be portably expressed with , the integer value 0 to the type void* (see pointer to void type).ISO/IEC 9899, clause 6.3.2.3, paragraph 3. Since C23, a null pointer is represented with nullptr which is of type nullptr_t (first introduced to C++11), providing a type safe null pointer.

The C standard does not say that the null pointer is the same as the pointer to  0, though that may be the case in practice. a null pointer is undefined behavior in C, and a conforming implementation is allowed to assume that any pointer that is dereferenced is not null.

In practice, dereferencing a null pointer may result in an attempted read or write from that is not mapped, triggering a segmentation fault or memory access violation. This may manifest itself as a program crash, or be transformed into a software exception that can be caught by program code. There are, however, certain circumstances where this is not the case. For example, in x86 , the address 0000:0000 is readable and also usually writable, and dereferencing a pointer to that address is a perfectly valid but typically unwanted action that may lead to undefined but non-crashing behavior in the application; if a null pointer is represented as a pointer to that address, dereferencing it will lead to that behavior. There are occasions when dereferencing a pointer to address zero is intentional and well-defined; for example, code written in C for 16-bit real-mode x86 devices may write the interrupt descriptor table (IDT) at physical address 0 of the machine by dereferencing a pointer with the same value as a null pointer for writing. It is also possible for the to optimize away the null pointer dereference, avoiding a segmentation fault but causing other undesired behavior.


C++
In C++, while the NULL macro was inherited from C, the integer literal for zero has been traditionally preferred to represent a null pointer constant.
(2001). 9780201889543, Addison–Wesley.
However, C++11 introduced the explicit null pointer constant nullptr and type nullptr_t to be used instead, providing a type-safe null pointer. nullptr and type nullptr_t were later introduced to C in C23.


Other languages
Programming languages use different literals for the null pointer. In Java and C#, the literal null is provided as a literal for reference types. In Java, the type of null is a special internal "null type", which has no name and cannot be referenced or accessed. In the API, however, it can be represented by . In C# (as of version 3.0), it is typeless, but may be cast to any type. In Pascal and Swift, a null pointer is called nil. In Eiffel, it is called a void reference. In Rust, the absence of a value is denoted as None (full name std::option::Option::None), but a true null pointer can be received from std::ptr::null<T>(), which is of type *const T.


Null dereferencing
Because a null pointer does not point to a meaningful object, an attempt to (i.e., access the data stored at that memory location) a null pointer usually (but not always) causes a run-time error or immediate program crash. lists the null pointer error as one of the most commonly exploited software weaknesses.

  • In C, dereferencing a null pointer is undefined behavior.ISO/IEC 9899, clause 6.5.3.2, paragraph 4, esp. footnote 87. Many implementations cause such code to result in the program being halted with an , because the null pointer representation is chosen to be an address that is never allocated by the system for storing objects. However, this behavior is not universal. It is also not guaranteed, since compilers are permitted to optimize programs under the assumption that they are free of undefined behavior. This behaviour is the same in C++, as there is no null pointer exception in the C++ language. On platforms such as systems and with the compiler, an access violation causes a C/C++ [[SIGSEGV]] signal to be issued. Although in C/C++ null dereferences are not exceptions which can be caught in C++ try/catch blocks, it is possible to "catch" such an access violation by using (std::)signal() in C/C++ to specify a handler to be called when that signal is issued.
    • In the POCO C++ Libraries, a Poco::NullPointerException exception is provided which is thrown on operations in the library that were passed a nullptr.
  • In Cyclone, a failed null pointer check will throw a Null_Exception.
  • In D, much like C++, a null pointer dereference results in a segmentation fault.
  • In Delphi and many other Pascal implementations, the constant nil represents a null pointer to the first address in memory, which is also used to initialize managed variables. Dereferencing it raises an external OS exception which is mapped onto a Pascal exception instance if the unit is linked in the uses clause.
  • In Java, access to a null reference (null) causes a (NPE), which can be caught by error handling code, but the preferred practice is to ensure that such exceptions never occur. In the Java Virtual Machine, "implicit null checks" may use virtual memory to avoid runtime checks and rely on the OS to catch rare null accesses via signals instead. The JVM uses profiling to switch between these fast implicit checks, and explicit checks if null is frequent enough.
  • In .NET and C#, access to null reference (null) causes a to be thrown. Although catching these is generally considered bad practice, this exception type can be caught and handled by the program.
  • In , messages may be sent to a nil object (which is a null pointer) without causing the program to be interrupted; the message is simply ignored, and the return value (if any) is nil or 0, depending on the type.
  • In Rust, dereferencing a null pointer (which can be received from std::ptr::null<T>(), which is of type *const T) in an unsafe block results in undefined behaviour, which usually results in a segmentation fault or corrupted memory.
  • Before the introduction of Supervisor Mode Access Prevention (SMAP), a null pointer dereference bug could be exploited by mapping into the attacker's and hence causing the null pointer to point to that region. This could lead to code execution in some cases.


Mitigation
While there could be languages with no nulls, most do have the possibility of nulls so there are techniques to avoid or aid debugging null pointer dereferences.
(2026). 9781595937865
Bond et al. suggest modifying the Java virtual machine (JVM) to keep track of null propagation.

There are three levels of handling null references, in order of effectiveness:

  1. languages with no null;
  2. languages that can statically analyse code to avoid the possibility of null dereference at run time;
  3. if null dereference can occur at runtime, tools that aid debugging.

Pure functional languages are an example of level 1 since no direct access is provided to pointers and all code and data is immutable. User code running in interpreted or virtual-machine languages generally does not suffer the problem of null pointer dereferencing.

Where a language does provide or utilise pointers which could become void, it is possible to avoid runtime null dereferences by providing via or other techniques, with syntactic assistance from language features such as those seen in the Eiffel programming language with to avoid null dereferences, D, and Rust. Nullable type systems have also been widely adopted in mainstream programming languages: Kotlin, Swift, and include them from the start, while C# and Scala have been retrofitted with nullable type systems.

In some languages, analysis can be performed using external tools, but these are weak compared to direct language support with compiler checks since they are limited by the language definition itself.

The last resort of level 3 is when a null reference occurs at runtime; debugging aids can help.


History
In 2009, stated that he invented the null reference in 1965 as part of the language. In that 2009 reference Hoare describes his invention as a "billion-dollar mistake":


See also


Notes
Page 1 of 1
1
Page 1 of 1
1

Account

Social:
Pages:  ..   .. 
Items:  .. 

Navigation

General: Atom Feed Atom Feed  .. 
Help:  ..   .. 
Category:  ..   .. 
Media:  ..   .. 
Posts:  ..   ..   .. 

Statistics

Page:  .. 
Summary:  .. 
1 Tags
10/10 Page Rank
5 Page Refs
1s Time